package com.sinoglobal.ningxia.activity.live;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.alibaba.fastjson.JSON;
import com.orhanobut.logger.Logger;
import com.sinoglobal.networkconnections.ResultCallback;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.activity.player.GSYVideoPlayerActtivity;
import com.sinoglobal.ningxia.activity.player.LivePlayerActivity;
import com.sinoglobal.ningxia.activity.player.ReporterLiveActivity;
import com.sinoglobal.ningxia.adapter.LiveHomeAdapter;
import com.sinoglobal.ningxia.beans.LiveHomeBean;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.utils.FlyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XINDZ on 2017/2/7.
 * 功能:看直播模块首页
 */

public class LiveHomeActivity extends BaseActivity implements AbOnListViewListener {
    List<LiveHomeBean.ListBean> liveHomeBeanList = new ArrayList();

    private AbPullListView aplvHome;
    private LiveHomeAdapter liveHomeAdapter;
    /**
     * 直播预告
     */
    private View headView;
    /**
     * HeadView是否存在的状态 存为true
     */
    private boolean HeadViewState = false;
    private RelativeLayout llHead;
    private TextView tvHead;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_livehome);
        openChenJInShi();
        //title
        titleView.setText("直播");

        //初始化AbPullListView
        aplvHome = (AbPullListView) findViewById(R.id.aplv_livehome);
        headView = LayoutInflater.from(this).inflate(R.layout.head_livehome, null);
        //设置头部
        llHead = (RelativeLayout) headView.findViewById(R.id.rl_head_livehome);
        tvHead = (TextView) headView.findViewById(R.id.tv_head_livehome);


        llHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FlyUtil.intentForward(LiveHomeActivity.this, LiveForeshowActivity.class);
                liveHomeAdapter.notifyDataSetChanged();
            }
        });
        //设置刷新
        aplvHome.getHeaderView().setHeaderProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        aplvHome.setPullLoadEnable(false);
        aplvHome.setAbOnListViewListener(this);
        TextView tvEmpty = (TextView) findViewById(R.id.tv1);

        tvEmpty.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                loadDate();
            }
        });
        aplvHome.setEmptyView(tvEmpty);
        loadDate();
        aplvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int itemId = 0;
                if (i != 0) {
                    if (!HeadViewState) {
                        itemId = i - 1;//刷新头 -1
                    } else {
                        itemId = i - 2;//刷新头 和添加的头部站0 1
                    }
                }
                Logger.i("itemID:" + itemId + "$i:" + i);
                LiveHomeBean.ListBean liveListBean = (LiveHomeBean.ListBean) liveHomeAdapter.getItem(itemId);
                int state = liveListBean.getState();
                switch (state) {
                    case 1://正在直播
                        toPlayActivity(liveListBean);
                        break;
                    case 2://未开始直播
                        toPlayActivity(liveListBean);//测试暂时开放
                        break;
                    case 3://直播结束
                        toPlayActivity(liveListBean);
                        break;
                    default:
                        break;
                }

            }
        });

    }

    private void toPlayActivity(LiveHomeBean.ListBean liveListBean) {
        String type = liveListBean.getType();
        String reporter = liveListBean.getReporter();
        String name = liveListBean.getName();
        if (type != null && type.equals("tv")) {
            Intent intentVideo = new Intent(LiveHomeActivity.this, GSYVideoPlayerActtivity.class);
//            Intent intentVideo = new Intent(LiveHomeActivity.this, VideoPlayerActivity.class);
            intentVideo.putExtra(LiveHomeBean.class.getCanonicalName(),
                    liveListBean);
            FlyUtil.intentForward(LiveHomeActivity.this, intentVideo);
        } else if (type != null && type.equals("reporter")) {
            if (reporter != null && reporter.equals("1")) {
                Intent intentReporterLive = new Intent(LiveHomeActivity.this, ReporterLiveActivity.class);
                intentReporterLive.putExtra(LiveHomeBean.class.getCanonicalName(),
                        liveListBean);
                FlyUtil.intentForward(LiveHomeActivity.this, intentReporterLive);
            } else {
                Intent intentLive = new Intent(LiveHomeActivity.this, LivePlayerActivity.class);
                intentLive.putExtra(LiveHomeBean.class.getCanonicalName(),
                        liveListBean);
                FlyUtil.intentForward(LiveHomeActivity.this, intentLive);
            }

        }
    }

    /**
     * 添加直播预告
     *
     * @param con
     */
    private void addForeShow(int con) {
        tvHead.setText("有" + con + "场直播即将开始");
        aplvHome.addHeaderView(headView);
        HeadViewState = true;//改变headView存在状态
    }


    private void loadDate() {
        progressDialog = ProgressDialog.show(this, null, "正在加载", true, true);
        //初始化数据
        ResultCallback resultCallback = new ResultCallback(this) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);

                LiveHomeBean liveHomeBean = JSON.parseObject(response, LiveHomeBean.class);
                if (liveHomeBean != null) {
                    int code = liveHomeBean.getCode();
                    int con = liveHomeBean.getCon();//直播预告con是预告条数
                    if (code == 0) {
                        if (con > 0) {
                            if (!HeadViewState) {//不存在
                                addForeShow(con);//添加头
                            }
                        } else {
                            aplvHome.removeHeaderView(headView);//移除
                            HeadViewState = false;//不添加头
                        }
                        List<LiveHomeBean.ListBean> liveListBean = liveHomeBean.getList();
                        if (liveListBean != null) {
                            liveHomeBeanList = liveListBean;
                            initAdapter();
                        }

                    } else {
                        showShortToastMessage(liveHomeBean.getMessage());
                    }
                }
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onErrorCode(Exception e) {
                super.onErrorCode(e);
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
            }
        };
        RemoteImpl.getInstance().getLiveHome(this).execute(resultCallback);


    }

    private void initAdapter() {
        //初始化Adapter
        if (liveHomeAdapter == null) {
            liveHomeAdapter = new LiveHomeAdapter(this, liveHomeBeanList);
        } else {
            liveHomeAdapter.setData(liveHomeBeanList);
        }

        aplvHome.setAdapter(liveHomeAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onRefresh() {
        aplvHome.stopRefresh();
        liveHomeBeanList.clear();
        loadDate();
    }

    @Override
    public void onLoadMore() {

    }
}
